package com.cqc.other.juc.volatil.demo3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DataEntity {

    private volatile int count = 0;

    private Lock lock = new ReentrantLock();

    public void add() {
        for (int i = 0; i < 1000000; i++) {
            lock.lock();
            try {
                count++;
            } finally {
                lock.unlock();
            }
        }
        //或者 for循环前加锁
//        lock.lock();
//        try {
//            for (int i = 0; i < 1000000; i++) {
//                count++;
//            }
//        } finally {
//            lock.unlock();
//        }
    }


    public int getCount() {
        return count;
    }
}
